﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Web;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Aparts.Web.Modules {
    public partial class ApartmentGallery : System.Web.UI.UserControl {

        #region Fields

        private IApartmentPhoto[] photos;

        #endregion

        #region Properties

        public bool UseJavascript { get; set; }

        #endregion

        #region Methods

        public void Populate(IApartmentGroup apartments) {
            this.photos = blackhouse.BlackApart.Business.Factory.Instance().GetApartmentPhoto().GetApartmentTypePhotos(apartments.Id).OrderBy(p => (p.PhotoId == apartments.MainPhoto.PhotoId) ? 0 : 1).ThenBy(p => p.Sort).ToArray();

            if (this.photos.Length == 0) {
                this.Visible = false;
                return;
            }

            if (this.UseJavascript) {
                JQueryLibraries jql = new JQueryLibraries();
                jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.gallery.js,blackhouse.Aparts.Web");

                ClientScriptManager js = this.Page.ClientScript;
                js.RegisterClientScriptBlock(typeof(ApartmentGallery), "ApartmentGallery" + apartments.Id, "<script language=\"javascript\" type=\"text/javascript\">" + this.GetJsCode(apartments) + "</script>");
               
                
                this.rptPhotos.Visible = false;
                this.pnlDynamicPanel.Visible = true;

                this.ltlTotalCount.Text = this.photos.Length.ToString();
                this.ltlDescription.Text = this.photos.First().Description;

                this.lnkToApartmentPage.NavigateUrl = UrlUtils.GetApartmentTypeUrl(apartments, DisplayApartmentTypeView.Default, 1);
            }
            else {
                this.rptPhotos.DataSource = photos;
                this.rptPhotos.DataBind();
            }
        }

        protected void rptPhotos_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    IApartmentPhoto photo = e.Item.DataItem as IApartmentPhoto;
                    HyperLink lnkToImage = e.Item.FindControl("lnkToImage") as HyperLink;

                    if (photo != null && lnkToImage != null) {
                        lnkToImage.NavigateUrl = UrlUtils.ApartmentPhotoBigImageUrl(photo.PhotoName);
                        lnkToImage.ImageUrl = UrlUtils.ApartmentPhotoSmallImageUrl(photo.PhotoName, new System.Drawing.Size(940, 900));
                        lnkToImage.ToolTip = photo.Description;
                    }
                    break;
            }
        }

        private string GetJsCode(IApartmentGroup apartments) {
            StringBuilder sb = new StringBuilder();

            string jsVar = this.pnlDynamicPanel.ClientID;

            sb.AppendFormat("var {0} = new Object();", jsVar); sb.AppendLine();
            sb.AppendFormat("{0}.PanelSelector = '#{1}';", jsVar, this.pnlDynamicPanel.ClientID); sb.AppendLine();
            sb.AppendFormat("{0}.ApartmentTypeId = {1};", jsVar, apartments.Id); sb.AppendLine();
            sb.AppendFormat("{0}_Images = new Array();", jsVar); sb.AppendLine();

            foreach (IApartmentPhoto photo in photos) {
                var jsPhotoVar = jsVar + '_' + photo.PhotoId;
                sb.AppendFormat("var {0} = new Object();", jsPhotoVar); sb.AppendLine();
                sb.AppendFormat("{0}.PhotoId = {1}", jsPhotoVar, photo.PhotoId); sb.AppendLine();
                sb.AppendFormat("{0}.Url = '{1}'", jsPhotoVar, UrlUtils.ApartmentPhotoBigImageUrl(photo.PhotoName)); sb.AppendLine();
                sb.AppendFormat("{0}.DisplayUrl = '{1}'", jsPhotoVar, UrlUtils.ApartmentPhotoSmallImageUrl(photo.PhotoName, new System.Drawing.Size(940, 940))); sb.AppendLine();
                sb.AppendFormat("{0}.Description = '{1}'", jsPhotoVar, photo.Description); sb.AppendLine();
                sb.AppendFormat("{0}_Images.push({1});", jsVar, jsPhotoVar);

            }
            sb.AppendFormat("{0}.Images = {0}_Images;", jsVar); sb.AppendLine();
            sb.AppendFormat("var ApartmentGallery = (typeof(ApartmentGallery) == 'undefined') ? new Array() : ApartmentGallery;");
            sb.AppendFormat("ApartmentGallery.push({0});", jsVar);
            return sb.ToString();
        }

        #endregion

    }
}